//
//  SAAddSupplierViewCell.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/7/2.
//

#import "SAAddSupplierViewCell.h"
#import "UILabel+NSMutableAttributedString.h"

@interface SAAddSupplierViewCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UITextField *rightTf;

@end

@implementation SAAddSupplierViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        [self initUI];
    }
    return self;
}

-(void)refreshLeftTitle:(NSString *)leftTitle RightTitle:(NSString *)rightTitle {
    if ([leftTitle isEqualToString:@"名称"]) {
        self.titleLab.text = @"名称*";
        [self.titleLab setAttributedStringWithSubString:@"*" color:[UIColor redColor]];
    }else {
        self.titleLab.text = leftTitle;
    }
    self.rightTf.keyboardType = UIKeyboardTypeDefault;
    if ([leftTitle isEqualToString:@"手机"]) {
        self.rightTf.keyboardType = UIKeyboardTypeNumberPad;
    }
    self.rightTf.placeholder = [NSString stringWithFormat:@"请输入%@",leftTitle];
    self.rightTf.text = rightTitle;
}

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
    if (string.length == 0) {
        return YES;
    }
    if ([self.titleLab.text isEqualToString:@"手机"]) {
        if (textField.text.length > 10) {
            return NO;
        }
    }
    return YES;
}

- (void)textFieldEditChanged:(UITextField *)textField {
    !self.inputBlock ?: self.inputBlock(textField.text);
}

-(void)initUI {
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.rightTf];
    [self.contentView addSubview:self.line];
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(30, 0, 80, 40)];
        _titleLab.textColor = kBlackColor;
        _titleLab.font = kBFont(15);
    }
    return _titleLab;
}

-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:SARectMake(0, 40, kBaseScreenWidth, 1)];
        _line.backgroundColor = kLineColor;
    }
    return _line;
}

-(UITextField *)rightTf {
    if (!_rightTf) {
        _rightTf = [[UITextField alloc] initWithFrame:SARectMake(120, 0, 232, 40)];
        _rightTf.font = kFont(15);
        _rightTf.textColor = kBlackColor;
        _rightTf.textAlignment = NSTextAlignmentRight;
        _rightTf.keyboardType = UIKeyboardTypeNumberPad;
        _rightTf.delegate = self;
        [_rightTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _rightTf;
}

@end
